package org.example.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.example.ResultVo;
import org.example.config.CookieConfig;
import org.example.dianguanjia.HttpUtils;
import org.example.entity.TempContract;
import org.example.entity.TempContractInfo;
import org.example.entity.TempCtrtequip;
import org.example.entity.TempPrice;
import org.example.mapper.TempContractMapper;
import org.example.mapper.TempCtrtequipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ============================================================<br>
 * <p> ProjectName ：dianguanjiaexportdata </p >
 * <p> @author lz </p >
 * <p> @date 2023/3/17 </p >
 * <p> @version ：1.0 </p >
 * ============================================================<br>
 * <p>
 * 获取系统合同信息
 * </p >
 * ============================================================<br>
 */
@Service
@Slf4j
public class CongtractServiceImpl {
    
    @Autowired
    private TempContractInforServiceImpl tempContractInfoService;
    
    @Autowired
    private TempPriceServiceImpl tempPriceService;
    
    @Autowired
    private TempContractServiceImpl tempContractService;
    
    @Autowired
    private TempCtrtequipServiceImpl tempCtrtequipService;
    @Autowired
    private TempCtrtequipMapper tempCtrtequipMapper;
    /**
     * 合同列表
     */
    private final String post_queryContractParameterList = "http://222.223.186.60:18020/energy-server/v1/restContractParameterController/queryContractParameterList";
    
    /**
     * 合同详情
     */
    private final String post_queryCtrtStateRecord = "http://222.223.186.60:18020/energy-server/v1/restContractParameter281Controller/queryCtrtStateRecord";
    private String queryContractParameter="http://222.223.186.60:18020/energy-server/v1/restContractParameterController/queryContractParameter";

    private final String cookie = CookieConfig.COOKIE;

    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 合同列表
     */
    public void queryContractParameterList() throws JsonProcessingException {
        Map<String, Object> param = new HashMap<>();
        param.put("pageFlag", false);
        param.put("ciCity", null);
        param.put("ctrtPage", "cons200");
        /*param.put("currentPageNum", 10);
        param.put("pageSize", 1);
        */
        String s= HttpUtils.httpPost(post_queryContractParameterList,param,cookie);
        ResultVo<List<TempContractInfo>> result = objectMapper.readValue(s, new TypeReference<ResultVo<List<TempContractInfo>>>() {
        });
        List<TempContractInfo> list = result.getData();
        tempContractInfoService.saveBatch(list);
    }
    
    /**
     * 合同详情
     */
    public void contractInfo() throws JsonProcessingException {
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //合同列表
        List<TempContractInfo> contractInfos = tempContractInfoService.allList();
        if(contractInfos==null ){
            log.error("合同信息为空");
        }
        //查询合同信息参数
        Map<String, Object> param = new HashMap<>();
        //合同信息集合
        List<TempContract> tempContractList = new ArrayList<>();
        //套餐信息集合
        List<TempPrice> priceList = new ArrayList<>();
        //设备信息集合
        List<TempCtrtequip> ctrtequipList = new ArrayList<>();
        for (TempContractInfo contractInfo : contractInfos) {
            param.put("ciId", contractInfo.getCiId());
            String s= HttpUtils.httpPost(post_queryCtrtStateRecord,param,cookie);
            ResultVo<TempContract> result = objectMapper
                    .readValue(s, new TypeReference<ResultVo<TempContract>>() {
                    });
            TempContract data = result.getData();
            if(data==null){
                log.info("合同信息不存在：{}",contractInfo.getCiId());
                continue;
            }
            tempContractList.add(data);
            //套餐信息
            if(data.getPriceList()!=null && !data.getPriceList().isEmpty()){
                data.getPriceList().stream().forEach(d->d.setCiId(data.getCiId()));
                priceList.addAll(data.getPriceList());
            }
            //设备信息
            if(data.getCtrtEquips()!=null && !data.getCtrtEquips().isEmpty()){
                data.getCtrtEquips().stream().forEach(d->d.setCiId(data.getCiId()));
                ctrtequipList.addAll(data.getCtrtEquips());
            }
            tempContractService.save(data);
            tempPriceService.saveBatch(data.getPriceList());
            tempCtrtequipService.saveBatch(data.getCtrtEquips());
        }
       /* tempContractService.saveBatch(tempContractList);
        tempPriceService.saveBatch(priceList);
        tempCtrtequipService.saveBatch(ctrtequipList);*/
    }

     /**
     * 二次更新那些没有变压器序列号的设备信息
     */
    public void updateByqBmXlh() {
        List<TempCtrtequip> tempCtrtequips = tempCtrtequipMapper.selectAll();
        for (int ii=0;ii<tempCtrtequips.size();ii++){
            TempCtrtequip l = tempCtrtequips.get(ii);
            if(l.getElecLv()==null){
                String ciId = l.getCiId();
                Map<String,Object> param=new HashMap<>();
                param.put("ciId",ciId);
                //页面容量  根据合同id从电管家特意查询相关的设备数据  之前根据合同的接口获取的设备信息中没有序列号
                String s= HttpUtils.httpPost(queryContractParameter,param,cookie);
                JSONObject parse = (JSONObject) JSONObject.parse(s);
                JSONObject datas = (JSONObject) parse.get("data");
                System.out.println(s);
                JSONArray ctrtEquips =(JSONArray)  datas.get("ctrtEquips");
				//数据库中查询合同对应的相关的设备信息
                List<TempCtrtequip> tempCtrtequips1 = tempCtrtequipMapper.selectByMap(param);
				//对数据库中查询到的设备信息进行更新 更新设备序列号
                for (int i=0;i<ctrtEquips.size();i++){
                    JSONObject j= (JSONObject) ctrtEquips.get(i);
                    if(j==null){
                        continue;
                    }
                    String equipParaNo = (String) j.get("equipParaNo");
					//变压器序列号
                    String equipSeriesNo = (String) j.get("equipSeriesNo");
                    TempCtrtequip t=tempCtrtequips1.get(i);
                    String equipParaNo1 = t.getEquipParaNo();
                    String equipSeriesNo1 = t.getEquipSeriesNo();
                    if(equipParaNo.equals(equipParaNo1)&&null==equipSeriesNo1&&equipSeriesNo!=null){
                        t.setEquipSeriesNo(equipSeriesNo);
                        tempCtrtequipMapper.updateById(t);
                    }
                    System.out.println(j);
                }
                System.out.println(ii);
            }
        }
    }
 
}
